﻿<?php


class QueueApp extends BaseApp
{

    var $type = 'queue';
	
    function index()
    {

	    $TB_user = &$this->_model('user');
		$money = $TB_user->moneyuser();
        $this->assign('money', $money);					
		$TB_new = &$this->_model('queuetype');
				
		$data = $TB_new->show();	

        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);	

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
			
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
	        $this->assign('site_url',SITE_URL);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');	
		
        $this->display('user/queuetype_list.view.html');		
		
		
    }



	
    function queuelist()
    {	
        if(!isset($_GET['word']))
		{
		$_GET['word'] = '';
		}					
        if(!isset($_POST['word']))
		{
		$_POST['word'] = '';
		}
		if($_GET['word'])
		{
	    $word = trim($_GET['word']);		
		}
		if($_POST['word'])
		{
	    $word = trim($_POST['word']);		
		}	
		
	
		$id= intval($_GET['id']);
		$td = 0 ;
		$cd = 0 ;
		$pd = 0 ;
		$queuenum = QUEUENUM ;		
		$TB_new = &$this->_model('queuetype');
				
		$allqueuetype = $TB_new->allshow();		
        $allqueuetype = $allqueuetype['data'];
		$queuetype_id = array();
        foreach($allqueuetype as $key => $value)
		{
		   if($value['id'] == $id)
		   {
		      $queuetype_id = $value;
		   }
		}
		$td = $queuetype_id['table_id'] ;
		$cd = $queuetype_id['cat_id'] ;
		$pd = $queuetype_id['productid'] ;
		$temp = array();
		$sql = '';
		if($td && $cd && $pd)
		{
		   $sql = " table_id={$td}  and cat_id={$cd} and productid={$pd} ";
		}elseif ($td && $cd && !$pd)
		{
		    $sql = " table_id={$td}  and cat_id={$cd} ";
            foreach($allqueuetype as $key => $value)
		    {
		       if($value['table_id'] == $td && $value['cat_id'] == $cd && $value['productid'])
		       {
		          $sql .= ' and productid !='.$value['productid'].' ';
		       }
		    }		    
		}elseif ($td && !$cd && !$pd)
		{
		    $sql = " table_id={$td}   ";
            foreach($allqueuetype as $key => $value)
		    {
		       if($value['table_id'] == $td && !empty($value['cat_id']) )
		       {
		          $sql .= ' and cat_id !='.$value['cat_id'].' ';
		       }
		    }		
		}elseif (!$td && !$cd && !$pd)
		{
		   $sql = " queuemoney='".$queuetype_id['queuemoney']."' ";		
		}

		$allqueuedata = $TB_new->queueshow('',$sql,$id,$word);

        $this->assign('lw_data', $allqueuedata['data']);		
		$this->assign('lw_page', $allqueuedata['page']);
		
		$this->assign('word', $word);
					
        $this->assign('queuenum', $queuenum);	

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('qid', $id);	
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
			
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
	        $this->assign('site_url',SITE_URL);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');	
		
        $this->display('user/queue_list.view.html');		
		
    }

    function checkuser()
    {
		    $username = $_GET['name'];
			$pwd = $_GET['pwd'];
			$pid = $_GET['pid'];
			$padui_name = '排队卡号' .$pid .'的' ;					
			if(empty($username))
			{
		        echo $padui_name.'账号不能为空!';
				exit;	
			}
			if(!is_numeric($username))
			{
		        echo $padui_name.'账号格式错误!';
				exit;				
			}						
			if(empty($pwd))
			{
		        echo $padui_name.'密码不能为空!';
				exit;				
			}
			if(!is_numeric($pwd))
			{
		        echo $padui_name.'密码格式错误!';
				exit;				
			}
		    $db = & get_db();		
            $order = $db->get_one("SELECT * FROM `".DB_PREFIX."queueka` WHERE zhanghao = '{$username}' LIMIT 1");				
		    if(isset($order['id']) && $order['id'] )
			{	
               if($order['ispay']==1 && $order['userid'])
			   {
				echo $padui_name.'消费卡('.$username.')已经被使用!';
				exit;		   
			   }
			   
			   if($order['pwd'] && $order['pwd'] == $pwd)
			   {
			       $queuetypedata = $db->get_one("SELECT * FROM `".DB_PREFIX."queuetype` WHERE id = '".$order['queue_id']."' LIMIT 1");
				   if($queuetypedata['id'])
				   {
				      	$queuemoney = $queuetypedata['queuemoney'];
				        echo $padui_name.'消费卡('.$username.')面值为￥'.$queuemoney.'元，可以使用!';
				        exit;						
				   }else
				   {
			            echo '不存在此排队了！';
                        exit;				   
				   }			   			   			
			   }else
			   {
			    echo $padui_name.'密码错误!';
                exit;			   
			   }
			}else
			{
			   echo $padui_name.'消费卡('.$username.')不存在!';
               exit;			
			}								
    }



 
     function queueka()
    {	
	    if(!isset($_GET['do']))
		{
		  $_GET['do'] = '';
		}
		
	    if(intval($_GET['do']) == 100)
		{
		    $username = $_POST['username'];
			$pwd = $_POST['password'];
            $userarray = $pwdarray = $kaarray = $zmimaarray = array();
			foreach($username as $key => $value)
			{
			    if(!empty($value)|| !empty($pwd[$key] ) )
				{
				      $padui_name = '排队卡号' .$key .'的' ;
			          if(empty($value))
			          {
				        $notice = &get_message(array('msg'=>$padui_name.'账号不能为空!','url'=>'','title'=>'Notice'));
		                $notice ->display();	
			          }			
			          if(!is_numeric($value))
			          {
				        $notice = &get_message(array('msg'=>$padui_name.'账号格式错误!','url'=>'','title'=>'Notice'));
		                $notice ->display();	
			          }	
			          if(empty($pwd[$key]))
			          {
				        $notice = &get_message(array('msg'=>$padui_name.'密码不能为空!','url'=>'','title'=>'Notice'));
		                $notice ->display();	
			          }
			          if(!is_numeric($pwd[$key]))
			          {
				        $notice = &get_message(array('msg'=>$padui_name.'密码格式错误!','url'=>'','title'=>'Notice'));
		                $notice ->display();	
			          }					
				      $userarray[$key]=$value;
					  $pwdarray[$key]=$pwd[$key];
					  $kaarray[$value]= $key;
					  $zmimaarray[$value]= $pwd[$key] ;
				}
			
			}
		    $sql_str1 = '';
			foreach($userarray as $key => $value)
			{
			     $sql_str1 .= "  zhanghao = '{$value}' or" ;
			}
			$sql_str1 = substr($sql_str1, 0,-2);
			if(empty($sql_str1))
			{
			   	$notice = &get_message(array('msg'=>'请至少输入一个排队卡号!','url'=>'','title'=>'Notice'));
		        $notice ->display();
			}
			onethread();
						
		    $db = & get_db();		
            $order = $db->get_all("SELECT * FROM `".DB_PREFIX."queueka` WHERE {$sql_str1} LIMIT 6");
			$youxiao_ka_array = array();
            foreach($order as $key => $value)
			{
			   $padui_name = '排队卡号' .$kaarray[$value['zhanghao']] .'的' ;	
		       if(isset($value['id']) && $value['id'] )
			   {
                   if($value['ispay']==1 && $value['userid'])
			       {
				      $notice = &get_message(array('msg'=>$padui_name.'消费卡已经被使用!','url'=>'','title'=>'Notice'));
		              $notice ->display();			   
			       }
			       if($value['pwd'] && $value['pwd'] == $zmimaarray[$value['zhanghao']])
			       {
				   	  $youxiao_ka_array[] = $value ;	 	   
			       }else
			       {
			          $notice = &get_message(array('msg'=>$padui_name.'密码错误!','url'=>'','title'=>'Notice'));
		              $notice ->display();				   
			       }
			  }else
			  {
			      $notice = &get_message(array('msg'=>$padui_name.'账号不存在!','url'=>'','title'=>'Notice'));
		          $notice ->display();				
			  }			    
			}
			$tijiao = 0 ;
            foreach($youxiao_ka_array as $key => $value)
			{
                foreach($youxiao_ka_array as $key1 => $value1)
			   {
			       if( $value['queue_id'] != $value1['queue_id'] )
				   {
				   $tijiao = 1 ;
				   }
				 
			   }			    
				 
			}
			if($tijiao==1)
			{
			      $notice = &get_message(array('msg'=>'账号当中必须全部是一样面值的才能进行排队!','url'=>'','title'=>'Notice'));
		          $notice ->display();			
			}
			$ka_num = count($youxiao_ka_array);


			       $queuetypedata = $db->get_one("SELECT * FROM `".DB_PREFIX."queuetype` WHERE id = '".$youxiao_ka_array[0]['queue_id']."' LIMIT 1");
				   if($queuetypedata['id'])
				   {
       $sql = 'insert into '.DB_PREFIX.'queuemsg_1(uid,username,table_id,cat_id,productid,queuemoney,add_time,paytime,productmsg,totalmoney,onemoney,buynum,orderid,queueid,isqueue,isyushu,isyouhui) VALUES';	
	   $update_sql = ''; 
	   $kaka_str ='';
	                $num_kkk=0 ;
	                foreach($youxiao_ka_array as $key => $value)
					{
					   $num_kkk ++ ;
                       $time = time();
		               $user_id = $GLOBALS['SCup']->SCup_user_msg['user_id'];
		               $user_name = $GLOBALS['SCup']->SCup_user_msg['data']['user_name'];
					   $tb = $queuetypedata['table_id'];						
					   $ct = $queuetypedata['cat_id'];
					   $pi = $queuetypedata['productid'];
					   $queuemoney = $queuetypedata['queuemoney'];
					   $productmsg = '';
					   $kkmoneytotal = $queuetypedata['queuemoney'];
					   $productmoney = $queuetypedata['queuemoney'];
					   $buynum = 1;
					   $orderid = $value['zhanghao'];
					   $queueid = $value['queue_id'];
					   if($ka_num<6)
					   {
                       $isyushu = 1;
					   $isyouhui = 0 ;
					   $isqueue = 0 ; 					   
					   }else if($ka_num==6)
					   {
					       if($num_kkk==1)
						   {
                           $isyushu = 0;
					       $isyouhui = 1 ;
					       $isqueue = 1 ; 						     
						   }else
						   {
                           $isyushu = 0;
					       $isyouhui = 0 ;
					       $isqueue = 0 ; 							   
						   }
					   
					   }
			           $sql .= "('{$user_id}','{$user_name}','{$tb}','{$ct}','{$pi}','{$queuemoney}','{$time}','{$time}','{$productmsg}','{$productmoney}','{$kkmoneytotal}','{$buynum}','{$orderid}','{$queueid}','{$isqueue}','{$isyushu}','{$isyouhui}'),"; 
				   	   $update_sql .= " zhanghao='".$orderid."' or";
					   $kaka_str .= $orderid.",";
					}			      
                    $sql = substr($sql, 0,-1) ;
					$update_sql = substr($update_sql, 0,-2);
                    $kaka_str = substr($kaka_str, 0,-1);

	     
					if($db->query($sql)&& $db->query("UPDATE `".DB_PREFIX."queueka` SET ispay=1,pay_time={$time},userid={$user_id},username='{$user_name}' WHERE ".$update_sql ))
					{
		  	   $queue_num = QUEUENUM ;		
		       $yushu_cha_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isyushu=1 && isqueue=0 && queueid={$queueid}  ORDER BY add_time asc ");	
			   $isdayutotal = $db->get_one("SELECT COUNT(*) AS num  FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ");
		       $isdayutotal=$isdayutotal['num'];
               $total_num_isyushu = count($yushu_cha_data);
			   $num = intval($total_num_isyushu / ($queue_num-1)) ;
			   $quxiaoyushu = array();
			   $queue_num_1 = $queue_num;
			   if($isdayutotal>=$queue_num && $num)
			   {
				   if($isdayutotal==$queue_num)
				   {
					  $queue_num_1 = $queue_num + 1; 
				   }
				   $i = 0 ;
				   $sql_str = $sql_str1 = ''; 
				   foreach($yushu_cha_data as $key => $value)
				   {
					  $i++ ;
					  if($i<= ($num*($queue_num_1-1) ))
					  {
					    $quxiaoyushu[$value['id']] = $value['id'];
						 $sql_str.="id='".$value['id']."'  or ";
					  }else
					  {
					   break ;
					  }  
				   }
				   $sql_str = substr($sql_str, 0,-3);
		           $queue_is_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ORDER BY add_time asc  LIMIT 0,{$num} ");	
				   foreach($queue_is_data as $key => $value)
				   {
					  $sql_str1.="id='".$value['id']."'  or ";
				   }	
				   $sql_str1 = substr($sql_str1, 0,-3);	
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isyushu=0 WHERE ".$sql_str." ");
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isqueue=1 WHERE ".$sql_str1." ");
				   $start1 = 1 ;			
			   }
					
					
               $TB_user = &$this->_model('user');
               $ary=array(
				   'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				   'type' => '1',
				   'name' => '使用排队卡进行排队',
				   'intro' => '您使用了排队卡进行排队，排队卡账号是'.$kaka_str.'，进入排队时间为'.date("Y-m-d G:i:s", $time),
				   'money' => 0,
				   'time' => time(),
		        );
		        $TB_user->moneylog($ary);
					
			        $notice = &get_message(array('msg'=>'已经进入排队队列中!','url'=>'','title'=>'Notice'));
		            $notice ->display();					
					} else
					{
			        $notice = &get_message(array('msg'=>'失败!','url'=>'','title'=>'Notice'));
		            $notice ->display();						
					} 
					  
				   }else
				   {
			        $notice = &get_message(array('msg'=>'已经不存在此类型面值排队卡了!','url'=>'','title'=>'Notice'));
		            $notice ->display();					   
				   }
			    

		
		}
		
		require(ROOT_PATH . '/lib/nav.lib.php');
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
			
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
	        $this->assign('site_url',SITE_URL);	
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');	
		
        $this->display('user/queueka_user.view.html');		
    }
 
  
}


?>